// WorkflowMappingServiceImpl.java
package cqrtplm.service.impl;


import cqrtplm.entity.WorkflowMapping;
import cqrtplm.mapper.WorkflowMappingMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;

@Service
public class WorkflowMappingServiceImpl {

    @Autowired
    private WorkflowMappingMapper mapper;

    public List<WorkflowMapping> getAll() {
        return mapper.findAll();
    }

    public Optional<WorkflowMapping> getById(String id) {
        WorkflowMapping record = mapper.findById(id);
        return Optional.ofNullable(record);
    }

    @Transactional
    public void create(WorkflowMapping mapping) {
        mapper.insert(mapping);
    }

    @Transactional
    public void update(String id, WorkflowMapping updated) {
        if (!getById(id).isPresent()) {
            throw new RuntimeException("Record not found: " + id);
        }
        updated.setWorkFlowContainerId(id);
        mapper.updateById(updated);
    }

    @Transactional
    public void delete(String id) {
        if (!getById(id).isPresent()) {
            throw new RuntimeException("Record not found: " + id);
        }
        mapper.deleteById(id);
    }
}